package com.mdd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdd.common.CustomException;
import com.mdd.mapper.CategoryMapper;
import com.mdd.pojo.Category;
import com.mdd.pojo.Dish;
import com.mdd.pojo.Setmeal;
import com.mdd.service.CategoryService;
import com.mdd.service.DishService;
import com.mdd.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {


    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;


    /**
     * 根据id进行删除分类，先判断该分类是否有依赖
     * @param id
     * @return
     */
    @Override
    public void remove(Long id) {
        //查询当前分类是否关联了菜品，如果关联了，抛出一个业务异常
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //根据分类id查询
        queryWrapper.eq(Dish::getCategoryId,id);
        int count = dishService.count(queryWrapper);
        if (count>0){
            throw new CustomException("当前分类下关联了菜品，不能删除!");
        }
        //查询当前分类是否关联了套餐，如果关联了，抛出一个业务异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据分类id查询
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(setmealLambdaQueryWrapper);
        if (count1 > 0) {
            throw new CustomException("当前分类下关联了套餐，不能删除!");
        }
        //删除该分类
        super.removeById(id);
    }
}
